package cn.core.sap.modules.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.core.sap.dao.mapper.Sap_tcodeMapper;
import cn.core.sap.dao.po.Sap_tcodePO;
import cn.osworks.aos.core.asset.AOSCons;
import cn.osworks.aos.core.asset.AOSUtils;
import cn.osworks.aos.core.id.AOSId;
import cn.osworks.aos.core.typewrap.Dto;
import cn.osworks.aos.core.typewrap.Dtos;

/**
 * <b>事务码管理[sap_tcode] controller</b>
 * <p>
 * </p>
 * 
 * @author penghe
 * @date 2016-08-17 16:53:10
 */
@Service
public class Sap_tcodeService {
	@Autowired
	private Sap_tcodeMapper sap_tcodemapper;
	
	/** 保存信息事务码管理
	 * @param inDto
	 * @return
	 */
	@Transactional
	public Dto saveSap_tcode(Dto inDto) {
		Dto outDto = Dtos.newDto();
		Sap_tcodePO sap_tcodePO = new Sap_tcodePO();
		AOSUtils.copyProperties(inDto, sap_tcodePO);
                          	    if (!checkIsExist(sap_tcodePO)) {
			outDto.setAppCode(AOSCons.ERROR);
			String msg = sap_tcodePO.getTcode_id_()+" "+sap_tcodePO.getType_()+" "+"已被存在, 请重新输入。";
			outDto.setAppMsg(msg);
			return outDto;
		}
 		sap_tcodePO.setId_(AOSId.uuid());
		sap_tcodemapper.insert(sap_tcodePO);
		outDto.setAppCode(AOSCons.SUCCESS);
		outDto.setAppMsg("操作完成，新增成功。");
		return outDto;
	}
	/** 更新事务码管理
	 * @param inDto
	 * @return
	 */
	@Transactional
	public Dto updateSap_tcode(Dto inDto) {
		Dto outDto = Dtos.newDto();
		Sap_tcodePO sap_tcodePO = new Sap_tcodePO();
		AOSUtils.copyProperties(inDto, sap_tcodePO);
		sap_tcodemapper.updateByKey(sap_tcodePO);
		outDto.setAppCode(AOSCons.SUCCESS);
		outDto.setAppMsg("操作完成，更新成功。");
		return outDto;
	}
	/** 删除事务码管理
	 * @param inDto
	 * @return
	 */
	@Transactional
	public Dto delete(Dto qDto) {
		Dto outDto = Dtos.newDto();
		String[] selections = qDto.getRows();
		int del = 0;
		for (String id_ : selections) {
			sap_tcodemapper.deleteByKey(id_);
			del++;
		}
		String msg = "操作完成, ";
		if (del > 0) {
			msg = AOSUtils.merge(msg + "成功删除[{0}]个。", del);
		}
		outDto.setAppMsg(msg);
		return outDto;
	}
	/** 判断存在事务码管理
	 */
	public boolean checkIsExist(Sap_tcodePO sap_tcodePO) {
		Dto calcDto = Dtos.newCalcDto("COUNT(id_)");
            calcDto.put("tcode_id_", sap_tcodePO.getTcode_id_()); 
                 calcDto.put("type_", sap_tcodePO.getType_()); 
  		Integer countInteger = Integer.valueOf(sap_tcodemapper.calc(calcDto));
		boolean out = true;
		if (countInteger > 0) {
			out = false;
		}
		return out;
	}
	
}
